//
//  PFHistoryViewController.m
//  PFScan
//
//  Created by perfayMini on 2018/10/24.
//  Copyright © 2018 perfayMini. All rights reserved.
//

#import "PFHistoryViewController.h"
#import "HistoryManager.h"
#import "PFScanResultViewController.h"
static NSString * Identifier = @"HistoryCell";


@interface PFHistoryViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)NSArray * histories;
@end

@implementation PFHistoryViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self config];
    // Do any additional setup after loading the view.
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.tableView reloadData];
}

- (void)config{
    self.navigationItem.title = @"历史记录";
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:Identifier];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(kNavigation_TOP_Margin);
        make.left.right.equalTo(self.view);
        make.bottom.equalTo(self.view).offset(-kTabBarHeight);
    }];
}


#pragma mark -------------   UITableViewDelegate,UITableViewDataSource

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:Identifier forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.textLabel.text = self.histories[indexPath.row];
    return cell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.histories.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    PFScanResultViewController * resultv = [[PFScanResultViewController alloc] init];
    resultv.result = self.histories[indexPath.row];
    [resultv setHidesBottomBarWhenPushed:YES];
    [self.navigationController pushViewController:resultv animated:YES];
}


- (NSArray *)histories{

    return [[HistoryManager shareHistoryManager] histories];
}

@end
